<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html xmlns:th="" lang="zh-CN">
<%@include file="/WEB-INF/pages/base/include.jsp"%>
<body>
<div id="wrapper">
    <%@include file="/WEB-INF/pages/base/menu.jsp"%>
    <div id="page-wrapper">
        <div class="row">
            <div class="col-lg-12">
                <h3 class="page-header">商品管理</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-lg-12 col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">商品规格组合列表</div>
                    <div class="panel-body inner-body">
                        <div class="main-box-search">
                        	<a class="btn btn-primary" href="${webPath}toProductFormatPriceAdd/${productId}" style="margin-left:15px;margin-bottom:30px;">添加规格组合</a>
                        	<a class="btn btn-primary" href="${webPath}toProductFormat/${productId}" style="margin-left:15px;margin-bottom:30px;">商品规格列表</a>
                        </div>
                        <div class="dataTable_wrapper">
                            <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                <thead>
	                                <tr>
	                                	<th class="text-center" style="width:5%;">id</th>
	                                    <th class="text-center" style="width:5%;">商品id</th>
	                                    <th class="text-center" style="width:10%;">尺寸</th>
	                                    <th class="text-center" style="width:10%;">颜色</th>
	                                    <th class="text-center" style="width:10%;">款式</th>
	                                    <th class="text-center" style="width:10%">加工方式</th>
	                                    <th class="text-center" style="width:12%">基础价格(￥)</th>
	                                    <th class="text-center" style="width:8%;">规格状态</th>
	                                    <th class="text-center" style="width:10%;">创建时间</th>
	                                    <th class="text-center" style="width:8%;">操作</th>
	                                </tr>
                                </thead>
                                <tbody id="productList">
                                
                                </tbody>
                            </table>
                        </div>
                        <div class="modal fade" id="delcfmModel">
                            <div class="modal-dialog modal-sm">
                                <div class="modal-content message_align">
                                    <div class="modal-header">
                                        <button class="close" type="button" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">提示信息</h4>
                                    </div>
                                    <div class="modal-body">
                                        <p>您确认要删除吗？</p>
                                    </div>
                                    <div class="modal-footer">
                                        <input id="url" type="hidden">
                                        <button class="btn btn-default" type="button" data-dismiss="modal">取消</button>
                                        <a class="btn btn-danger" onclick="pro_delete()" data-dismiss="modal">确定</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="text-right">
                            <ul class="pagination" id="pagination">
                                <li><a href="javascript:void(0);" onclick="" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>
				                <li><a  href="javascript:void(0);" onclick="" >1</a></li>
				                <li><a href="javascript:void(0);" onclick=""  aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script th:inline="javascript">
	searchCase(0,1);
	
	$('#searchForm').change(function(e) {
		searchCase(1,1);
	})
	
	var currentPage = 1;
	
	//搜索
	function searchCase(flag,page) {
		currentPage = page;
		
		var productId = "${productId}";
		var pageSize= 10;
		var baseURL = "${webPath}/product/listOfFormatPriceAll";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId":productId,
	            "page": page,
	            "pageSize": pageSize
	        }
	    }
	    $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
	        if (typeof result == 'string') result = JSON.parse(result);
	        if (result.body.result == '1') {
	            alert(result.body.description)
	            $("#productList").empty();
	        } else {
	        	$("#productList").empty();
	        	var list = result.body.list;
	        	console.log(list);
	        	var count = result.body.count;
	        	var str = "";
	        	if(list.length<=0){
	        		return
	        	}
	        	for(var i=0;i<list.length;i++){
	        		str += '<tr>';
	        		str += '	<td class="text-center">'+list[i].id+'</td>';
	        		str += '	<td class="text-center">'+list[i].productId+'</td>';
	        		str += '	<td class="text-center">'+list[i].sizeName+'</td>';
	        		str += '	<td class="text-center">'+list[i].colorName+'</td>';
	        		str += '	<td class="text-center">'+list[i].styleName+'</td>';
	        		str += '	<td class="text-center">'+list[i].ppmName+'</td>';
	        		str += '	<td class="text-center"><input type="text"  id="basePrice'+i+'" value="'+list[i].basePrice+'"></td>';
	        		str += '	<td class="text-center">'+list[i].isActiveValue+'</td>';
	        		str += '	<td class="text-center">'+list[i].createTime+'</td>';
	        		str += '	<td class="text-center">';
	        		str += '		<a class="btn btn-default btn-delete" href="#" onclick=modify("'+list[i].productId+'","'+list[i].productSizeId+'","'+list[i].productColorId+'","'+list[i].productStyleId+'","'+list[i].productProcessingMethodsId+'","'+i+'")>修改价格</a>';
	        		str += '		<a class="btn btn-default btn-delete" href="#" onclick=delcfm("'+list[i].id+'")>删除</a>';
	        		str += '	</td>';
	        		str += '</tr>';
	        	}
	        	$("#productList").append(str);
	        	
	        	$("#pagination").empty();
	        	var pageString = "";
	        	var currentPage = page;
	        	var sumPage = Math.ceil(count/pageSize);
	        	pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+(currentPage-1)+') aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
	        	for(var i=1;i<=sumPage;i++){
        			if(currentPage == i){
        				pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+i+') class="active">'+i+'</a></li>';
        			}else{
        				pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+i+')>'+i+'</a></li>';
        			}
	        	}
	        	if(sumPage==currentPage){
	        		pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+(currentPage)+') aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
	        	}else{
	        		pageString += '<li><a href="javascript:void(0);" onclick=searchCase(1,'+(currentPage+1)+') aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
	        	}
	        	
	        	$("#pagination").append(pageString);
	        }
	    })
	}


	
	var formatIddCurrent = "";
	
    function delcfm(id) {
        $('#delcfmModel').modal();
        formatIddCurrent = id;
    }
    var pro_delete = function () {
    	var baseURL = "${webPath}/product/deleteFormatPrice";
	    var json_package = {
	        "head": {},
	        "body": {
	        	"id": formatIddCurrent
	        }
	    }
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('删除成功')
                searchCase(1,currentPage);
            } else {
                alert(result.body.description)
            }
        })
    }

    function modify(productId,productSizeId,productColorId,productStyleId,productProcessingMethodsId,basePriceId) {
    	var basePrice = $("#basePrice"+basePriceId).val();
    	var baseURL = "${webPath}/product/modifyFormatPriceNew";
    	
	    var json_package = {
	        "head": {},
	        "body": {
	        	"productId": productId,
	        	"productSizeId": productSizeId,
	        	"productColorId": productColorId,
	        	"productStyleId": productStyleId,
	        	"productProcessingMethodsId": productProcessingMethodsId,
	        	"basePrice": basePrice
	        }
	    }
        $.post(baseURL, {"json_package": JSON.stringify(json_package)}, function (result) {
            if (typeof result == 'string') result = JSON.parse(result);
            if (result.body.result == '0') {
                alert('修改成功')
                searchCase(1,currentPage);
            } else {
                alert(result.body.description)
            }
        })
    }
    
    $(document).ready(function () {
        var lenth = $('#product_list tr').length;
        if (lenth == 0) {
            $('tbody#menu_list').append('<tr id="empty_menu"><td colspan="7" class="no-menu text-center">没有商品信息！</td></tr>');
        } else {
            $('#empty_menu').hide();
        }
    })

</script>
</body>
</html>
